﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblCust_MemberDAL:ItblCust_Member
    {
        HKEA_CMSEntities model;
        tblCust_Member member;

        public tblCust_MemberDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblCust_MemberDAL GetInstance()
        {
            return new tblCust_MemberDAL();
        }

        public tblCust_Member GetENT(int uid)
        {
            return model.tblCust_Member.Where(a => a.MemberUID == uid).SingleOrDefault() ;
        }

        public int InsertENT(tblCust_Member ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblCust_Member.AddObject(ent);
            return model.SaveChanges();
        }

        public List<tblCust_Member> GetListByENT(tblCust_Member ent, ref PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblCust_Member.Where(a => a.MemberUID > 0);
            if (ent.MemberUID > 0)
                result = result.Where(a => a.MemberUID == ent.MemberUID);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.MemTypeUID > 0)
                result = result.Where(a => a.MemTypeUID == ent.MemTypeUID);
            if (ent.MemStatusUID > 0)
                result = result.Where(a => a.MemStatusUID == ent.MemStatusUID);
            if (ent.Deleted >= 0)
                result = result.Where(a => a.Deleted == ent.Deleted);
            if (!string.IsNullOrEmpty(ent.MemberID))
                result = result.Where(a => a.MemberID == ent.MemberID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);

            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip(param.RecordStart).Take(param.PageSize).Distinct();
            return result.ToList();
        }

        public List<tblCust_Member> GetListByENT(tblCust_Member ent)
        {
            var result = model.tblCust_Member.Where(a => a.MemberUID > 0);
            if (ent.MemberUID > 0)
                result = result.Where(a => a.MemberUID == ent.MemberUID);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.MemTypeUID > 0)
                result = result.Where(a => a.MemTypeUID == ent.MemTypeUID);
            if (ent.MemStatusUID > 0)
                result = result.Where(a => a.MemStatusUID == ent.MemStatusUID);
            if (ent.Deleted >= 0)
                result = result.Where(a => a.Deleted == ent.Deleted);
            if (!string.IsNullOrEmpty(ent.MemberID))
                result = result.Where(a => a.MemberID == ent.MemberID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.OrderByDescending(a => a.ExpiryDate).ToList();
        }

        public int GetCountByENT(tblCust_Member ent)
        {
            var result = model.tblCust_Member.Where(a => a.MemberUID > 0);
            if (ent.MemberUID > 0)
                result = result.Where(a => a.MemberUID == ent.MemberUID);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (ent.MemTypeUID > 0)
                result = result.Where(a => a.MemTypeUID == ent.MemTypeUID);
            if (ent.MemStatusUID > 0)
                result = result.Where(a => a.MemStatusUID == ent.MemStatusUID);
            if (ent.Deleted >= 0)
                result = result.Where(a => a.Deleted == ent.Deleted);
            if (!string.IsNullOrEmpty(ent.MemberID))
                result = result.Where(a => a.MemberID == ent.MemberID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblCust_Member ent)
        {
            member = model.tblCust_Member.Where(a=>a.MemberUID==ent.MemberUID).FirstOrDefault();
            if (member != null)
            {
                member.Deleted = ent.Deleted;
                member.LastModifyBy = ent.LastModifyBy;
                member.LastModifyDate = DateTime.Now;
                member.CustomerUID = ent.CustomerUID;
                member.ExpiryDate = ent.ExpiryDate;
                member.JoinDate = ent.JoinDate;
                member.MemberID = ent.MemberID;
                member.MemStatusUID = ent.MemStatusUID;
                member.MemTypeUID = ent.MemTypeUID;
                member.TerminationDate = ent.TerminationDate;
                member.WithdrawDate = ent.WithdrawDate;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblCust_Member ent)
        {
            var temp = model.tblCust_Member.Where(a => a.MemberUID > 0);
            if (ent.MemberUID > 0)
                temp = temp.Where(a=>a.MemberUID==ent.MemberUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblCust_Member.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
